package List;

public class MyLinkedList707 {
    public static void main(String[] args) {
        MyLinkedList707 linkedList = new MyLinkedList707();
        linkedList.addAtIndex(1,0);
        System.out.println(linkedList.get(0));
    }
    int size = 0;
    ListNode head = null;

    public MyLinkedList707() {
        head = new ListNode(0);
    }

    public int get(int index) {
        if (index < 0 || index >= size) {
            return -1;
        }
        ListNode pos = head;
        for (int i = 0; i <= index; i++) {
            pos = pos.next;
        }
        return pos.val;
    }

    public void addAtHead(int val) {
        addAtIndex(0, val);
    }

    public void addAtTail(int val) {
        addAtIndex(size, val);
    }

    public void addAtIndex(int index, int val) {
        if (index > size) {
            return;
        } else if (index < 0) {
            index = 0;
        }
        ListNode pos = head;
        for (int i = 0; i < index; i++) {
            pos = pos.next;
        }

        ListNode newNode = new ListNode(val);
        newNode.next = pos.next;
        pos.next = newNode;

        size++;

    }

    public void deleteAtIndex(int index) {
        if (index < 0 || index >= size) {
            return;
        }
        size--;
        ListNode pos = head;
        for (int i = 0; i < index; i++) {
            pos = pos.next;
        }
        pos.next = pos.next.next;

    }
}
